<template>
  <div class="app-container">
    <el-form :model="formData" label-width="120px" label-position="left">
      <el-form-item label="年份">
        <el-select v-model="formData.year" placeholder="请选择年份">
          <el-option
            v-for="item in yearList"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科目">
        <el-select v-model="formData.subject" placeholder="请选择科目">
          <el-option
            v-for="item in subjectList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="批次">
        <el-select v-model="formData.batch" placeholder="请选择批次">
          <el-option
            v-for="item in batchList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="院校">
        <el-select v-model="formData.collegeId" placeholder="请选择院校">
          <el-option
            v-for="item in collegeList"
            :key="item.collegeId"
            :label="item.collegeName"
            :value="item.collegeId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="专业名称">
        <el-input v-model="formData.majorName"></el-input>
      </el-form-item>
      <el-form-item label="专业代码">
        <el-input v-model="formData.majorCode"></el-input>
      </el-form-item>
      <el-form-item label="计划人数">
        <el-input v-model="formData.planNum"></el-input>
      </el-form-item>
      <el-form-item label="录取人数">
        <el-input v-model="formData.actualNum"></el-input>
      </el-form-item>
      <el-form-item label="录取最高分">
        <el-input v-model="formData.maxScore"></el-input>
      </el-form-item>
      <el-form-item label="录取最低分">
        <el-input v-model="formData.minScore"></el-input>
      </el-form-item>
    </el-form>
    <div class="submit-box">
      <el-button type="primary" :loading="loading" style="width: 200px;" @click.native.prevent="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
  import { edit, getDetail } from '@/api/admission_data'
  import { getAll } from '@/api/college'
  import { errorMsg, successMsg } from '@/utils/my'

  export default {
    data() {
      return {
        loading: false,
        formData: {
          id: 0,
          year: '',
          subject: '',
          batch: '',
          collegeId: '',
          majorName: '',
          majorCode: '',
          planNum: '',
          actualNum: '',
          maxScore: '',
          minScore: ''
        },
        yearList: [],
        subjectList: [
          {id: 1, name: '文科'},
          {id: 2, name: '理科'}
        ],
        batchList: [
          {id: 1, name: '本科一批'},
          {id: 2, name: '本科二批'},
          {id: 3, name: '高职高专批'}
        ],
        collegeList: []
      }
    },

    created() {
      this.formData.id = this.$route.params.id
      this.fetchCollegeData()
      this.fetchData()
    },

    methods: {

      fetchData() {
        let that = this
        getDetail({id: that.formData.id}).then(res => {
          that.formData.year = res.data.admissionDataYear
          that.formData.subject = res.data.admissionDataSubject
          that.formData.batch = res.data.admissionDataBatch
          that.formData.collegeId = res.data.admissionDataCollegeId
          that.formData.majorName = res.data.admissionDataMajorName
          that.formData.majorCode = res.data.admissionDataMajorCode
          that.formData.planNum = res.data.admissionDataPlanNum.toString()
          that.formData.actualNum = res.data.admissionDataActualNum.toString()
          that.formData.maxScore = res.data.admissionDataMaxScore.toString()
          that.formData.minScore = res.data.admissionDataMinScore.toString()
        })
      },

      doSubmit() {
        const that = this
        let d = that.formData
        if (d.year.length <= 0) {
          return errorMsg('请选择年份')
        }
        if (d.subject.length <= 0) {
          return errorMsg('请选择科目')
        }
        if (d.batch.length <= 0) {
          return errorMsg('请选择科目')
        }
        if (d.collegeId.length <= 0) {
          return errorMsg('请选择大学')
        }
        if (d.majorName.length <= 0) {
          return errorMsg('请输入专业名称')
        }
        if (d.majorCode.length <= 0) {
          return errorMsg('请输入专业代码')
        }
        if (d.planNum.length <= 0) {
          return errorMsg('请输入计划人数')
        }
        if (d.actualNum.length <= 0) {
          return errorMsg('请输入录取人数')
        }
        if (d.maxScore.length <= 0) {
          return errorMsg('请输入录取最高分')
        }
        if (d.minScore.length <= 0) {
          return errorMsg('请输入录取最低分')
        }
        that.loading = true
        edit(that.formData).then(res => {
          that.loading = false
          if (res.code != 20000) {
            return errorMsg(res.message)
          }
          successMsg(res.message)
          setTimeout(function() {
            that.$router.push({path: '/admin/admission-data/list'})
          }, 1500)
        })
      },

      fetchCollegeData() {
        const that = this
        getAll().then(res => {
          that.collegeList = res.data
        })
      },

      genYearList() {
        const that = this
        let d = new Date(),
            curYear = d.getFullYear()
        for (let i = 2000; i <= curYear; i++) {
          that.yearList.push(i)
        }
      },

    }
  }
</script>

<style>
  .submit-box {
    height: 50px;
    line-height: 50px;
    text-align: center;
    padding-right: 20px;
  }
</style>
